package org.extremengine.demo;

import java.awt.Graphics;
import java.awt.event.KeyEvent;

import org.extremengine.common.ActiveBody;
import org.extremengine.common.RigidBody;
import org.extremengine.common.Velocity;
import org.extremengine.shape.ParallelRectangle;
import org.extremengine.swing.LongPressKeyListener;

public class PanelMario extends BasePanelWorld {

	private ActiveBody mario;
	private RigidBody box1;
	private RigidBody box2;

	public PanelMario(DemoCallback demoCallback) {
		super(demoCallback);
		
		//按键控制
		this.addKeyListener(new LongPressKeyListener() {
			@Override
			public void keyDown(KeyEvent e) {
				System.out.printf("KeyDown: %d  %d \r\n", e.getKeyCode(), Thread.currentThread().getId());
				int keyCode = e.getKeyCode();
				
				// 在地面上
				if(mario.isStickyWith(ground)) {
					if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {
						mario.walk(-15);
					}
					else if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {
						mario.walk(15);
					}
					else if(keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_W) {
						mario.jump(25);
					}					
				}
				// 在空中
				else {
					if (keyCode == KeyEvent.VK_LEFT || keyCode == KeyEvent.VK_A) {
						mario.getV().setSpeedx(-15);
					}
					else if (keyCode == KeyEvent.VK_RIGHT || keyCode == KeyEvent.VK_D) {
						mario.getV().setSpeedx(15);
					}
				}
				
				// 空格键
//				if (keyCode == KeyEvent.VK_SPACE) {
//					if (world.getStatus() == TimerThread.STATUS_RUNNING) {
//						world.pause();
//					}
//					else if (world.getStatus() == TimerThread.STATUS_PAUSED) {
//						world.resume();
//					}
//				}
			}

			@Override
			public void keyUp(KeyEvent e) {
				System.out.printf("KeyUp: %d \r\n", e.getKeyCode(), Thread.currentThread().getId());
				mario.setV(new Velocity(0, 0));
			}

		});
	}

	@Override
	public void initDemoWorld() {
		super.initDemoWorld();
		mario = world.createActiveBody(new Object(), 0.5, 0.5, 50, new ParallelRectangle(2, 3.0, 10, 13));
		mario.setMovable(true);

		box1 = world.createRigidBody(new Object(), 0.5, 0.5, 200, new ParallelRectangle(10, 20, 0, 5));
		
		box2 = world.createRigidBody(new Object(), 0.5, 0.5, 300, new ParallelRectangle(25, 30, 0, 8));
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (!world.isRunning()) {
			this.drawRigidBody(g, mario);
			this.drawRigidBody(g, box1);
			this.drawRigidBody(g, box2);
		}
	}

}
